package group_items

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"strconv"
)

var _ GroupItemsModel = (*customGroupItemsModel)(nil)

type (
	// GroupItemsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customGroupItemsModel.
	GroupItemsModel interface {
		groupItemsModel
		FindGroupId(ctx context.Context, deptid, roomid, doctorid, seatid string) (int64, error)
		FindGroupItems(ctx context.Context, pageIndex, pageSize int64) ([]*GroupItemType, error)
		FindGroupItemsByDeptId(ctx context.Context, deptId string) ([]*GroupItemId, error)
	}

	customGroupItemsModel struct {
		*defaultGroupItemsModel
	}
)

// NewGroupItemsModel returns a model for the database table.
func NewGroupItemsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) GroupItemsModel {
	return &customGroupItemsModel{
		defaultGroupItemsModel: newGroupItemsModel(conn, c, opts...),
	}
}

type QueueGroupId struct {
	GroupId sql.NullString `db:"groupid"` //总数
}

// 查找对应的分组id
func (m *defaultGroupItemsModel) FindGroupId(ctx context.Context, deptid, roomid, doctorid, seatid string) (int64, error) {
	var resp GroupItems
	if roomid == "" {
		roomid = "0"
	}
	if doctorid == "" {
		doctorid = "0"
	}
	if seatid == "" {
		seatid = "0"
	}
	//query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", groupItemsRows, m.table)
	query := fmt.Sprintf("select groupid,itemid,groupItems.id from group_items groupItems,queue_item item,queue_groups g where groupItems.groupid=g.id and item.id=groupItems.itemid and item.deptid=? and item.roomid=? and item.doctorid=? and item.seatid=? limit 1")
	err := m.QueryRowNoCache(&resp, query, deptid, roomid, doctorid, seatid)
	if err != nil {
		return -1, err
	}
	groupId, err := strconv.ParseInt(resp.Groupid.String, 10, 64)
	return groupId, err
}

type GroupItemType struct {
	GroupId    int64          `db:"groupid"`    // 队列结束时间 12:00:00
	Groupname  sql.NullString `db:"groupname"`  // 队列结束时间 12:00:00
	Deptid     sql.NullString `db:"deptid"`     // 科室id
	Deptname   sql.NullString `db:"deptname"`   // 科室名称
	Roomid     sql.NullString `db:"roomid"`     // 房间ID
	Roomname   sql.NullString `db:"roomname"`   // 房间名称
	Doctorid   sql.NullString `db:"doctorid"`   // 医生id
	Doctorname sql.NullString `db:"doctorname"` // 医生名称
	Starttime  sql.NullString `db:"starttime"`  // 队列开始时间 8:00
	Endtime    sql.NullString `db:"endtime"`    // 队列结束时间 12:00
}

// FindGroupItems 查找对应的分组id
func (m *defaultGroupItemsModel) FindGroupItems(ctx context.Context, pageIndex, pageSize int64) ([]*GroupItemType, error) {
	page := (pageIndex - 1) * pageSize
	var resp []*GroupItemType
	query := fmt.Sprintf(`select qg.id as groupid,qg.name as groupname,deptid,deptname,roomid,roomname,doctorid,doctorname,starttime,endtime from callsystem.group_items ci,callsystem.queue_item qi,callsystem.queue_groups qg where ci.itemid=qi.id and ci.groupid=qg.id limit ?,?`)
	err := m.QueryRowsNoCache(&resp, query, page, pageSize)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

type GroupItemId struct {
	GroupId  int64          `db:"groupid"`  // 队列结束时间 12:00:00
	Deptid   sql.NullString `db:"deptid"`   // 科室id
	Roomid   sql.NullString `db:"roomid"`   // 房间ID
	Doctorid sql.NullString `db:"doctorid"` // 医生id
}

// FindGroupItemsByDeptId 根据科室ID查找对应的分组
func (m *defaultGroupItemsModel) FindGroupItemsByDeptId(ctx context.Context, deptId string) ([]*GroupItemId, error) {
	var resp []*GroupItemId
	sql := fmt.Sprintf("SELECT groupId,deptid,roomid,doctorid FROM callsystem.group_items as gi,callsystem.queue_item item where gi.id=item.id and item.deptid=?")
	err := m.QueryRowsNoCache(&resp, sql, deptId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}
